﻿using System;
using System.Data.SqlClient;
using System.Data;


namespace Hanhud.Data
{
    public class CDbAccess
    {
        private Type _connectionManagerType = typeof(CDbConnectionManager);
        private string _objectQualifier = null;
        private string _dbOwner = null;

        public CDbAccess()
        {
            _dbOwner = Core.CBaseConfig.Current.DbSchema;
            _objectQualifier = Core.CBaseConfig.Current.DbObjectQualifier;
        }

        public CDbAccess(string dbSchema, string objectQualifier)
        {
            _dbOwner = dbSchema;
            _objectQualifier = objectQualifier;
        }

        public string GetObjectName(string objectName)
        {
            return string.Format("{0}.[{1}{2}]", _dbOwner, _objectQualifier, objectName);
        }

        public string GetObjectQualifier()
        {
            return _objectQualifier;
        }

        public void SetObjectQualifier(string objectQualifier)
        {
            _objectQualifier = objectQualifier;
        }

        public int ExecuteNonQuery(SqlCommand cmd)
        {
            // Thiet lap che do bao mat o day

            // Query counter
            Hanhud.Data.Utils.CQueryCounter qc = new Hanhud.Data.Utils.CQueryCounter(cmd.CommandText);
            // number of row affected
            int retv = 0;

            try
            {
                if (cmd.Connection != null)
                {
                    if (cmd.Connection.State != ConnectionState.Open) cmd.Connection.Open();
                }
                else cmd.Connection = GetConnectionManager().OpenDBConnection;

                retv = cmd.ExecuteNonQuery();
            }
            finally
            {
                if (cmd.Connection.State != ConnectionState.Closed) cmd.Connection.Close();

                qc.Dispose();
            }

            return retv;
        }

        public object ExecuteSalar(SqlCommand cmd)
        {
            // Query counter
            Hanhud.Data.Utils.CQueryCounter qc = new Hanhud.Data.Utils.CQueryCounter(cmd.CommandText);
            // number of row affected
            object retv = null;

            try
            {
                cmd.Connection = GetConnectionManager().OpenDBConnection;
                retv = cmd.ExecuteScalar();
            }
            finally
            {
                if (cmd.Connection.State != ConnectionState.Closed) cmd.Connection.Close();
                qc.Dispose();
            }

            return retv;
        }

        public IDataReader ExecuteReader(SqlCommand cmd)
        {
            return ExecuteReader(cmd, CommandBehavior.CloseConnection);
        }

        public IDataReader ExecuteReader(SqlCommand cmd, CommandBehavior behavior)
        {
            // Query counter
            Hanhud.Data.Utils.CQueryCounter qc = new Hanhud.Data.Utils.CQueryCounter(cmd.CommandText);
            // number of row affected
            IDataReader reader = null;

            try
            {
                reader = cmd.ExecuteReader(behavior);
            }
            finally
            {
                qc.Dispose();
            }

            return reader;
        }

        public CDbConnectionManager GetConnectionManager()
        {
            return (CDbConnectionManager)Activator.CreateInstance(_connectionManagerType);
        }

        public void SetConnectionManagerAdapter<T>()
        {
            Type newConnectionManager = typeof(T);

            if (newConnectionManager.BaseType == typeof(CDbConnectionManager))
            {
                _connectionManagerType = newConnectionManager;
            }
        }
    }
}
